#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10, M = 1e6 + 10;

int T;
int n;
int a[N];
int b[M];
int main()
{
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	
	cin >> T;
	
	while (T -- )
	{
		scanf("%d", &n);
		for (int i = 1; i <= n; ++ i )
		{
			scanf("%d", &a[i]);
			if (i != 1 && i != n)
			{
				b[a[i]] ++;
			}
		}
		bool flag = true;
		long long res = 0;
		for (int i = 1; i <= 1e6; ++ i )
		{
			if (b[i] >= 2)
			{
				flag = false;
				res += (b[i] - 1) * i;
			}
		}
		if (flag)
		{
			b[a[1]] ++;
			b[a[n]] ++;
			if (a[1] != a[n] && b[a[1]] >= 2)
			{
				res += a[1];
			}
			if (a[1] != a[n] && b[a[n]] >= 2)
			{
				res += a[n];
			}
			if (a[1] == a[n])
			{
				res += a[1];
			}
		}
		memset(b, 0, sizeof b);
		cout << res << endl;
		
	}
	
	return 0;
	
}
